package cn.demon.product.service.impl;

import cn.demon.basic.service.IBaiduAiAuditService;
import cn.demon.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.demon.basic.util.LoginContext;
import cn.demon.org.domain.Employee;
import cn.demon.org.mapper.EmployeeMapper;
import cn.demon.product.domain.Product;
import cn.demon.product.domain.ProductOnlineAuditLog;
import cn.demon.product.domain.ProductDetail;
import cn.demon.product.domain.Product;
import cn.demon.product.mapper.ProductDetailMapper;
import cn.demon.product.mapper.ProductMapper;
import cn.demon.product.mapper.ProductOnlineAuditLogMapper;
import cn.demon.product.service.IProductService;
import cn.demon.basic.service.impl.BaseServiceImpl;
import cn.demon.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        // 获取登录对象
        Logininfo logininfo = LoginContext.getLoginUser(request);

        // 审核
        // 获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            Boolean imageAudit = baiduAiAuditService.imageAudit(product.getResources());
            Boolean textAudit = baiduAiAuditService.textAudit(product.getName());
            System.out.println(imageAudit);
            System.out.println(textAudit);

            if (imageAudit && textAudit) {
                // 审核通过（修改状态 - 保存日志）
                Date date = new Date();
                product.setOnsaletime(date);
                product.setState((long) 1);
                productMapper.update(product);

                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditId(product.getId());
                log.setState(1);
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("恭喜皇上，贺喜皇上，审核通过！");
                productOnlineAuditLogMapper.save(log);

            }else{
                // 审核不通过 - 保存审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditId(product.getId());
                log.setState(-1);
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("您上传的内容让臣妾惶恐！恕臣妾做不到");
                productOnlineAuditLogMapper.save(log);
            }
        }

    }

    @Override
    public void offsale(Long[] ids) {

        Date offsaletime = new Date();
        productMapper.offsale(ids, offsaletime);
    }

    @Override
    public void del(Long id) {
        productMapper.remove(id);
        productDetailMapper.removeByProductId(id);
    }

    @Override
    public void add(Product product) {
        super.add(product);
        ProductDetail productDetail = product.getProductDetail();

        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();

        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.updateProductDetailByProductId(productDetail);
        }

    }
}
